package com.hsnn.medstgmini.settlement.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.settlement.model.form.SettleConfigForm;
import com.hsnn.medstgmini.settlement.service.SettleconfigManager;
import com.hsnn.medstgmini.sys.model.SysUser;
import com.hsnn.medstgmini.util.Pagination;

/**
 * 
 *@category 结算设置
 *@author 邱磊
 *@date 2015年7月22日 上午8:41:44
 */
@Controller
@RequestMapping("/settleconfig")
public class SettleconfigController {
	private static final Logger logger = Logger
			.getLogger(SettleconfigController.class);
	@Autowired
	private HttpServletRequest request;
	@Autowired
	private SettleconfigManager manager;
	/**
	 * 
	 *@category 跳向结算定时任务执行时间设置页面
	 *@author 邱磊
	 *@date 2015年7月22日 上午8:47:43
	 *@return
	 */
	@RequestMapping("/toSettleconfig")
	public String toSettleconfig(){
		String[] days = manager.getSettleConfigDays();
		StringBuffer daysString = new StringBuffer("");
		if(days.length > 0){
			for(int i = 0; i < days.length; i++){
				daysString.append(days[i].toString()+",");
			}
			daysString.deleteCharAt(daysString.lastIndexOf(","));
		}
		request.setAttribute("days", daysString.toString());
		return "/settle/settleconfig";
	}
	@RequestMapping("/saveSettleconfig")
	@ResponseBody
	public Pagination saveSettleconfig(SettleConfigForm form){
		Pagination page = new Pagination();
		if (StringUtils.isBlank(form.getConfigValue())){
			page.setSuccess(false);
			page.setMsg("执行日期设置至少选一天！");
			return page;
		}
		try {
			SysUser user = (SysUser) request.getSession().getAttribute(
					Constants.USERINFO);
			form.setUpdDatetime(new Date());
			form.setUpdUser(user.getId());
			manager.saveSettleconfig(form);
			page.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("保存结算定时任务设置失败：", e);
			page.setSuccess(false);
			page.setMsg("保存失败，请联系管理员！");
			return page;
		}
		return page;
	}
}
